<?php
/**
* @software         [JiJinCMS]
* @author           [上海集锦信息科技有限公司]
* @website          [http://www.jijinweb.com]
* @email            [sunny@jijinweb.com]
**/
namespace app\admin\controller;
use think\facade\View;
use think\facade\Db;
use think\facade\Request;
class Model extends \app\admin\controller\Admin{
    public function __construct(\think\App $app) {
        parent::__construct($app);
        $this->model=new \app\admin\model\ModelModel();
        $this->validate=new \app\admin\validate\ModelValidate();
    }
    public function index(){
        $table_id=Request::param("table_id");
        $data=$this->model->where(array("table_id"=>$table_id))->order('sort asc,modelid desc')->select();
        View::assign("table_id",$table_id);
        View::assign("data",$data);
        return View::fetch();
    }
    //更新排序
    public function updateSort(){
        $modelid=Request::param('modelid');
        $sort=Request::param('sort');
        foreach($modelid as $key=>$v){
            $this->model->update(['modelid'=>$modelid[$key],'sort'=>$sort[$key]]);
        }
        $this->success("设置成功");
    }       
    public function add(){
        if(request()->isPost()){
            $data=$this->request->param();
            if(!$this->validate->check($data)){
                $this->error($this->validate->getError());
            }
            $data["formtemp"]=$this->model->get_formtemp($data);
            $data["table_name"]=$this->model->get_table_name($data);
            $data["is_enter"]=$this->model->arr2string(Request::param('is_enter'));
            $data["is_contribute"]=$this->model->arr2string(Request::param('is_contribute'));
            $data["must_enter"]=$this->model->arr2string(Request::param('must_enter'));
            $data["is_list"]=$this->model->arr2string(Request::param('is_list'));
            $data["is_search"]=$this->model->arr2string(Request::param('is_search'));
            $data["is_sort"]=$this->model->arr2string(Request::param('is_sort'));
            $data['cat_formtemp']="栏目名称<!--field-->name
    缩略图<!--field-->thumb
    每页显示<!--field-->lencord
    列表模板<!--field-->listtemp
    内容模板<!--field-->viewtemp
    状态<!--field-->status
    SEO标题<!--field-->pagetitle
    关键词<!--field-->keywords
    描述<!--field-->description
    ";
            $data['cat_is_enter']=",name,thumb,lencord,listtemp,viewtemp,status,pagetitle,keywords,description,";
            $data['cat_must_enter']=",name,listtemp,viewtemp,status,";
            $this->model->save($data);
            $modelid=$this->model->modelid;
            $data['modelid']=$modelid;            
            $this->model->updateModelTemp($data);//更新后台模型缓存
            $this->model->updateModelTempMember($data);//更新会员投稿模型缓存
            $this->model->updateCatModelTemp($data);//默认增加模型会勾选栏目模型字段
            $this->model->updateCache();//更新模型缓存
            $this->UserLogModel->add(Request()->action(),$this->model->getName(),$modelid,$this->request->param());
            $this->success("添加成功");
        }else{      
            //
            $table_id= Request::param("table_id");
            //展示该数据表下所有的字段
            $table_field=Db::name("table_field")->where("table_id",$table_id)->order("sort asc,field_id asc")->select();     
            View::assign("table_field",$table_field);
            View::assign("table_id",$table_id);
            return View::fetch();
        }
    }
    public function update(){  
        if(request()->isPost()){
            $data=$this->request->param();
            //编辑状态下重置规则
            $this->validate->rule['name']['unique']='model,name,'.Request::param('modelid',0,"intval");  
            unset($this->validate->rule['table_id']);
            if(!$this->validate->check($data)){
                $this->error($this->validate->getError());
            }
            //
            $data["formtemp"]=$this->model->get_formtemp($data);
            $data["table_name"]=$this->model->get_table_name($data);
            $data["is_enter"]=$this->model->arr2string(Request::param('is_enter'));
            $data["is_contribute"]=$this->model->arr2string(Request::param('is_contribute'));
            $data["must_enter"]=$this->model->arr2string(Request::param('must_enter'));
            $data["is_list"]=$this->model->arr2string(Request::param('is_list'));
            $data["is_search"]=$this->model->arr2string(Request::param('is_search'));
            $data["is_sort"]=$this->model->arr2string(Request::param('is_sort'));
            $this->model->update($data);   
            $this->model->updateModelTemp($data);//更新后台模型缓存
            $this->model->updateModelTempMember($data);//更新会员投稿模型缓存
            $this->model->updateCache();//更新模型缓存
            $this->UserLogModel->add(Request()->action(),$this->model->getName(),$data['modelid'],$this->request->param());            
            $this->success("修改成功");
        }else{
            $modelid=Request::param('modelid');
            $r=$this->model->find($modelid);
            //将formtemp中的字段名称更新到字段列表中
            $field_name_arr=array();
            $field_arr=array();
            $formtype_arr=explode("\r\n",$r["formtemp"]);
            foreach($formtype_arr as $v){
                $f=explode("<!--field-->",$v);
                if(isset($f[1])){
                    $field_arr[]=$f[1];
                    $field_name_arr[$f[1]]=$f[0];
                }
            }
            $new_table_field=array();
            //展示该数据表下所有的字段
            $table_field=Db::name("table_field")->where("table_id",$r['table_id'])->order("sort asc,field_id asc")->select();  
            foreach($table_field as $v){
                //更新字段名
                if(in_array($v["field"], $field_arr)){
                    $v["name"]=$field_name_arr[$v["field"]];
                }
                //判断该字段分别在每个勾选项里是否有勾选
                $v["is_enter_checked"]=strstr($r['is_enter'],",".$v["field"].",")?1:0;
                $v["is_contribute_checked"]=strstr($r['is_contribute'],",".$v["field"].",")?1:0;
                $v["must_enter_checked"]=strstr($r['must_enter'],",".$v["field"].",")?1:0;
                $v["is_list_checked"]=strstr($r['is_list'],",".$v["field"].",")?1:0;
                $v["is_search_checked"]=strstr($r['is_search'],",".$v["field"].",")?1:0;
                $v["is_sort_checked"]=strstr($r['is_sort'],",".$v["field"].",")?1:0;
                //
                $new_table_field[]=$v;
            } 
            View::assign("table_field",$new_table_field);
            View::assign("r",$r);
            return View::fetch();
        }        
    }
    //编辑栏目字段
    public function editorCatModel(){
        if(request()->isPost()){
            $data["modelid"]=Request::param('modelid');
            $data["cat_is_enter"]=$this->model->arr2string(Request::param('cat_is_enter'));
            $data["cat_must_enter"]=$this->model->arr2string(Request::param('cat_must_enter'));
            //栏目模型字段
            $field_name=Request::param('field_name');
            $field=Request::param('field');
            $str="";
            foreach($field as $key=>$v){
                $str.=$field_name[$key]."<!--field-->".$field[$key]."\r\n";
            }        
            $data["cat_formtemp"]=$str;   
            $this->model->update($data);
            $this->model->updateCatModelTemp($data);
            //更新缓存数据
            $this->model->updateCache();  
            $this->success("修改成功");
        }else{
            $modelid=Request::param('modelid',0,"intval");  
            $r=$this->model->find($modelid);
            //将formtemp中的字段名称更新到字段列表中
            $field_name_arr=array();
            $field_arr=array();            
            $formtype_arr=explode("\r\n",$r["cat_formtemp"]);
            foreach($formtype_arr as $v){
                $f=explode("<!--field-->",$v);
                if(isset($f[1])){
                    $field_arr[]=$f[1];
                    $field_name_arr[$f[1]]=$f[0]; 
                }
            }
            $new_table_field=array();
            $table_field=Db::name("cat_field")->order("sort asc,field_id asc")->select();
            foreach($table_field as $v){
                //更新字段名
                if(in_array($v["field"], $field_arr)){
                    $v["name"]=$field_name_arr[$v["field"]];
                }
                //判断该字段分别在每个勾选项里是否有勾选
                $v["cat_is_enter_checked"]=strstr($r['cat_is_enter'],",".$v["field"].",")?1:0;
                $v["cat_must_enter_checked"]=strstr($r['cat_must_enter'],",".$v["field"].",")?1:0;
                //
                $new_table_field[]=$v;
            } 
            View::assign("table_field",$new_table_field);
            View::assign("modelid",$modelid);
            return View::fetch();        
        }
    }    
    //删除节点
    public function delete(){
        $modelid=Request::param('modelid');
        //检查该模型下是否有分类
        $cat = Db::name("cat")->where("modelid" ,$modelid)->find();
        if ($cat) {
            $this->error("该模型下有分类，请先删除分类！");
        }          
        $this->model->where("modelid",$modelid)->delete();
        //更新缓存数据
        $this->model->updateCache();
        //删除模型表单文件
        $this->model->deleteTemp($modelid);    
        $this->UserLogModel->add(Request()->action(),$this->model->getName(),$modelid,$this->request->param()); 
        $this->success("删除成功");
    } 
    //获取导入的excel模板
    public function GetImportExcelDemo(){
        //验证
        $validate = \think\facade\Validate::rule('modelid','require')
        ->rule([
            'modelid'  => 'require',
            'cat_type' => 'require',
            'import_field' => 'require',            
        ])->message([
            "modelid.require"=>"缺少modelid参数",            
            "cat_type.require"=>"请选择目标栏目类型",
            "import_field.require"=>"请勾选要导入的字段",
        ]);
        if (!$validate->check(Request::param())) {
            $this->error($validate->getError());
        }
        $this->model->GetImportExcelDemo(Request::param());
    }
}